<?php

namespace App\Http\Controllers\Backend;

use Illuminate\Http\Request;
use Overtrue\LaravelWeChat\Facade;

/**
 * Class WechatMediaController
 * @package App\Http\Controllers\Backend
 * 微信素材
 */
class WechatMediaController extends BackendController
{
    const TYPE_IMAGE = 'image';
    const TYPE_NEWS = 'news';

    /**
     * @param Request $request
     * @return array
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * 素材列表
     */
    public function index(Request $request)
    {
        $list = [];
        $app = Facade::officialAccount();
        $stats = $app->material->stats();
        if ($request->type == self::TYPE_IMAGE) {
            $list['images'] = $app->material->list('image', (($request->page - 1) * $request->size), $request->size);
            $list['images']['total'] = $stats['image_count'];
        } elseif ($request->type == self::TYPE_NEWS) {
            $list['news'] = $app->material->list('news', (($request->page - 1) * $request->size), $request->size);
            $list['news']['total'] = $stats['news_count'];
        }
        return $list;
    }
}